package com.yyfax.common.form;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.*;
import android.text.method.NumberKeyListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import com.yyfax.common.R;
import com.yyfax.common.tools.IDCardValidate;

/**
 * Created by fuqiang on 2017/5/4.
 * 带扫描按钮的输入框
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class FormScanView extends LinearLayout implements View.OnClickListener {

	private boolean canScan = true;

	/**
	 * 点击扫描回调(银行卡和身份证用到输入框扫描)
	 */
	private onScanClickListener mOnScanClickListener;

	/**
	 * 是否检查身份证的年龄(绑定银行卡页面不检查, 推荐贷款页面检查, 默认false, 不检查)
	 */
	private boolean mCheckAge;

	/**
	 * 是否开启扫描, 默认true，开启
	 */
	private boolean mCanScan = true;

	public ImageButton mIdCardIb;

	public ClearEditText mREditText;

	private OnFocusChangeListener mListener;

	TypedArray mTa;
	Drawable mFormScanViewErrorFlagIcon;
	Drawable mFormScanViewScanIcon;

	public FormScanView(Context context) {
		super(context);
	}

	public FormScanView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public FormScanView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	void init(Context context, AttributeSet attrs) {
		mTa = context.obtainStyledAttributes(attrs, R.styleable.FormRow);
		mCheckAge = mTa.getBoolean(R.styleable.FormRow_checkAge, false);
		mCanScan = mTa.getBoolean(R.styleable.FormRow_canScan, true);
		setOrientation(LinearLayout.HORIZONTAL);
		setGravity(Gravity.CENTER_VERTICAL);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.id_card_layout, this);
		mREditText = view.findViewById(R.id.id_card_edit_text);
		mREditText.setEllipsize(TextUtils.TruncateAt.END);
		int mType = mTa.getInt(R.styleable.FormRow_customType, 3);
		if (mType == FormType.TYPE_ID_CARD) {
			mREditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(18)});
			//mREditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			mREditText.setKeyListener(new NumberKeyListener() {
				@Override
				protected char[] getAcceptedChars() {
					return new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'x', 'X'};
				}

				@Override
				public int getInputType() {
					return InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
				}
			});

		} else {//银行卡
			mREditText.setInputType(InputType.TYPE_CLASS_NUMBER);
			int maxLength = mTa.getInt(R.styleable.FormRow_maxLength, 19);//设置最大输入长度
			mREditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
		}

		mREditText.setHint(mTa.getString(R.styleable.FormRow_hint));
		boolean rightTextBold = mTa.getBoolean(R.styleable.FormRow_rightTextBold, false);
		mREditText.getPaint().setFakeBoldText(rightTextBold);

		ColorStateList rightTextColorHint = mTa.getColorStateList(R.styleable.FormRow_textColorHint);//设置textcolorhint
		if (rightTextColorHint != null)
			mREditText.setHintTextColor(rightTextColorHint);
		else
			mREditText.setHintTextColor(getResources().getColor(R.color.color_cccccc));
		Drawable formScanViewBackground = mTa.getDrawable(R.styleable.FormRow_FormScanViewBackground);
		if (formScanViewBackground != null) {
			setBackgroundDrawable(formScanViewBackground);
		}
		ColorStateList rightTextColor = mTa.getColorStateList(R.styleable.FormRow_rightTextColor);//设置textcolor
		if (rightTextColor != null)
			mREditText.setTextColor(rightTextColor);
		else
			mREditText.setTextColor(getResources().getColor(R.color.color_000000));

		mIdCardIb = view.findViewById(R.id.id_card_ib);
		if (mCanScan) {
			mIdCardIb.setOnClickListener(this);
		} else {
			mIdCardIb.setImageDrawable(null);
		}
		int type = mTa.getInt(R.styleable.FormRow_customType, 7);
		if (type == FormType.TYPE_ID_CARD) {

			mREditText.setOnFocusChangeListener(new ClearEditText.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					String applicantLicense = mREditText.getText().toString();
					if (hasFocus) {//如果有焦点
						mFormScanViewScanIcon = mTa.getDrawable(R.styleable.FormRow_FormScanViewScanIcon);
						if (mFormScanViewScanIcon != null) {
							mIdCardIb.setImageDrawable(mCanScan ? mFormScanViewScanIcon : null);
						} else {
							mIdCardIb.setImageDrawable(mCanScan ? getContext().getResources().getDrawable(R.drawable.scan_ic) : null);
						}
						canScan = true;
					} else {//如果没有焦点
						mREditText.setCompoundDrawables(null, null, null, null);//不显示清空按钮
						if (!TextUtils.isEmpty(applicantLicense)) {//如果有内容
							if (!TextUtils.isEmpty(IDCardValidate.checkIdCard(applicantLicense, mCheckAge, getContext().getString(R.string.error_age_strs), true))) {//如果身份证不合法
								mFormScanViewErrorFlagIcon = mTa.getDrawable(R.styleable.FormRow_FormScanViewErrorFlagIcon);
								if (mFormScanViewErrorFlagIcon != null) {
									mIdCardIb.setImageDrawable(mFormScanViewErrorFlagIcon);
								} else {
									mIdCardIb.setImageDrawable(getContext().getResources().getDrawable(R.drawable.input_error_flag_icon));//感叹号图标
								}
								canScan = false;
							} else {//如果身份证合法
								mFormScanViewScanIcon = mTa.getDrawable(R.styleable.FormRow_FormScanViewScanIcon);
								if (mFormScanViewScanIcon != null) {
									mIdCardIb.setImageDrawable(mCanScan ? mFormScanViewScanIcon : null);
								} else {
									mIdCardIb.setImageDrawable(mCanScan ? getContext().getResources().getDrawable(R.drawable.scan_ic) : null);//扫描图标
								}
								canScan = true;
							}
						} else {//如果没有内容
							mFormScanViewScanIcon = mTa.getDrawable(R.styleable.FormRow_FormScanViewScanIcon);
							if (mFormScanViewScanIcon != null) {
								mIdCardIb.setImageDrawable(mCanScan ? mFormScanViewScanIcon : null);
							} else {
								mIdCardIb.setImageDrawable(mCanScan ? getContext().getResources().getDrawable(R.drawable.scan_ic) : null); //扫描图标
							}
							canScan = true;
						}
					}
				}
			});
		} else {
			mREditText.setOnFocusChangeListener(new ClearEditText.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if (mListener != null)
						mListener.onFocusChange(v, hasFocus);
				}
			});
		}
	}

	public void setCanEdit(boolean canEdit) {
		if (!canEdit) {
			mREditText.setFocusable(false);
			mREditText.setFocusableInTouchMode(false);
			mREditText.setKeyListener(null);
			mREditText.addTextChangedListener(new TextWatcher() {
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				}

				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
				}

				@Override
				public void afterTextChanged(Editable s) {
					String applicantLicense = mREditText.getText().toString();
					if (!TextUtils.isEmpty(applicantLicense)) {//如果有内容
						if (!TextUtils.isEmpty(IDCardValidate.checkIdCard(applicantLicense, mCheckAge, getContext().getString(R.string.error_age_strs), true))) {//如果身份证不合法
							mFormScanViewErrorFlagIcon = mTa.getDrawable(R.styleable.FormRow_FormScanViewErrorFlagIcon);
							if (mFormScanViewErrorFlagIcon != null) {
								mIdCardIb.setImageDrawable(mFormScanViewErrorFlagIcon);
							} else {
								mIdCardIb.setImageDrawable(getContext().getResources().getDrawable(R.drawable.input_error_flag_icon));//感叹号图标
							}
						} else {//如果身份证合法
							mFormScanViewScanIcon = mTa.getDrawable(R.styleable.FormRow_FormScanViewScanIcon);
							if (mFormScanViewScanIcon != null) {
								mIdCardIb.setImageDrawable(mFormScanViewScanIcon);
							} else {
								mIdCardIb.setImageDrawable(getContext().getResources().getDrawable(R.drawable.scan_ic));//扫描图标
							}
						}
					} else {//如果没有内容
						mFormScanViewScanIcon = mTa.getDrawable(R.styleable.FormRow_FormScanViewScanIcon);
						if (mFormScanViewScanIcon != null) {
							mIdCardIb.setImageDrawable(mFormScanViewScanIcon);
						} else {
							mIdCardIb.setImageDrawable(getContext().getResources().getDrawable(R.drawable.scan_ic)); //扫描图标
						}
					}
				}
			});
		}
	}

	@Override
	public void onClick(View v) {
		if (mOnScanClickListener != null)
			mOnScanClickListener.onScanClick(this);//扫描回调
	}

	public void setOnScanClickListener(onScanClickListener listener) {
		mOnScanClickListener = listener;
	}

	/**
	 * 点击扫描按钮回调
	 */
	public interface onScanClickListener {
		void onScanClick(View view);
	}


	public void setText(CharSequence text) {
		mREditText.setText(text);
	}

	public String getFormText() {
		return mREditText.getText().toString().trim();
	}

	public void clear() {
		mREditText.getText().clear();
	}

	public boolean ismCheckAge() {
		return mCheckAge;
	}

	public void addTextChangedListener(TextWatcher watcher) {
		mREditText.addTextChangedListener(watcher);
	}

	public interface OnFocusChangeListener {
		void onFocusChange(View v, boolean hasFocus);
	}

	public void setOnFocusChangeListener(OnFocusChangeListener listener) {
		this.mListener = listener;
	}
}
